package com.zhixinsou.wuyang.process;

import com.alibaba.fastjson.JSON;
import com.zhixinsou.wuyang.model.AnalysisReport;
import com.zhixinsou.wuyang.model.SheetModel;
import com.zhixinsou.wuyang.util.FileUtil;
import com.zhixinsou.wuyang.util.StringUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ProcessOPPENHEIMER {
    public static List<AnalysisReport> getAnalysisReportPageInfo(String filePath) {
        List<AnalysisReport> reports = new ArrayList<>();
        int startPage = 1;
        File file = new File(filePath);
        String[] lines = FileUtil.pdfReader(file, startPage, startPage).split("\r\n");
        Pattern pattern = Pattern.compile("([0-9]+)\\s-\\s([0-9]+)$");
        while (lines.length > 1 && lines[0].equals("Table of Contents")) {
            for (String line : lines) {
                Matcher matcher = pattern.matcher(line);
                if (matcher.find()) {
                    AnalysisReport report = new AnalysisReport();
                    report.setFilePath(filePath);
                    report.setFileName(file.getName());
                    report.setStartPage(Integer.parseInt(matcher.group(1)));
                    report.setEndPage(Integer.parseInt(matcher.group(2)));
                    report.setBrokerName("FAHNESTOCK & CO. INC.");
                    
                    reports.add(report);
                }
            }

            startPage++;
            lines = FileUtil.pdfReader(filePath, startPage, startPage).split("\r\n");
        }
        return reports;
    }

    public static void getAnalysisReportDetail(AnalysisReport report) {
        Pattern patternVersion = Pattern.compile("OPPENHEIMER_File\\s([0-9]+)[_]{0,1}");
        Matcher matcher = patternVersion.matcher(report.getFileName());
        if (matcher.find()) {
            int version = Integer.parseInt(matcher.group(1));
            if(version == 1)
                getAnalysisReportDetailVersion1(report);
            else if(version <= 15) {
                if(version == 15 && report.getStartPage() >= 122)
                    getAnalysisReportDetailVersion16_35(report);
                else
                    getAnalysisReportDetailVersion2_15(report);
            }
            else
                getAnalysisReportDetailVersion16_35(report);
        }
    }

    public static void getAnalysisReportDetailVersion1(AnalysisReport report) {
        String content = FileUtil.pdfReader(report.getFilePath(), report.getStartPage(), report.getStartPage());
        String[] lines = content.split("\r\n");

        //Date and CompanyName
        Pattern patternDate = Pattern.compile("(January|February|March|April|May|June|July|August|September|October|November|December){1}\\s*[0-9]+\\S\\s*[0-9]{4}");
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i];
            Matcher matcherDate = patternDate.matcher(line);
            if (matcherDate.find()) {
                report.setDate(matcherDate.group());
                report.setCompanyName(lines[i + 1].trim());
                break;
            }
        }

        //Rating
        Pattern patternRating = Pattern.compile("(Rating:){1}\\s([a-zA-Z]+)");
        Pattern patternRating2 = Pattern.compile("\\s([a-zA-Z]+)\\s(rating){1}");
        for (String line : lines) {
            Matcher matcherRating = patternRating.matcher(line);
            Matcher matcherRating2 = patternRating2.matcher(line);
            if (matcherRating.find()) {
                report.setRating(matcherRating.group(2));
                break;
            }
            if (matcherRating2.find()) {
                report.setRating(matcherRating2.group(1));
                break;
            }
        }

        //Analyst name
        Pattern patternName = Pattern.compile("(.*)(\\([0-9]+\\)\\s*[0-9]+\\-[0-9]+)");
        Pattern patternName2 = Pattern.compile(".*(Analyst:)");
        for (String line : lines) {
            Matcher matcherName = patternName.matcher(line);
            Matcher matcherName2 = patternName2.matcher(line);
            if (matcherName.find()) {
                report.addAnalystName(matcherName.group(1));
            }
            if(matcherName2.find()){
                int start = matcherName2.end(1);
                report.addAnalystName(line.substring(start));
            }
        }

        //Ticker
        Pattern patternTicker = Pattern.compile("([A-Z]+)\\-([A-Z]+).*($).*");
        Pattern patternTicker2 = Pattern.compile("\\((.*),\\s+(.*),\\s+(\\$).*\\)");
        for (String line : lines) {
            Matcher matcherTicker = patternTicker.matcher(line);
            Matcher matcherTicker2 = patternTicker2.matcher(line);
            if (matcherTicker.find()) {
                report.setTicker(matcherTicker.group(1));
                report.setExchange(matcherTicker.group(2));
                break;
            }
            if(matcherTicker2.find()){
                report.setTicker(matcherTicker2.group(1));
                report.setRating(matcherTicker2.group(2));
            }
        }

        //Prior recommendation
        String sentence = "";
        content = FileUtil.pdfReader(report.getFilePath(), report.getStartPage(), report.getStartPage(), false);
        lines = content.split("\r\n");
        for(int i = 0; i < lines.length; i++){
            String line = lines[i].trim();
            if(line.contains("Investment Opinion") || line.contains("Investment Overview")){
                sentence = line;
                i = i + 1;
                while(i < lines.length ){
                    sentence += lines[i];
                    if(lines[i].contains(".")){
                        break;
                    }else{
                        i++;
                    }
                }
                break;
            }
        }
        if(sentence.equals("")){
            content = FileUtil.pdfReaderWithFonts(report.getFilePath(), report.getStartPage(), report.getStartPage());
            lines = content.split("\r\n");
            for(int i = 0; i < lines.length; i++){
                if(lines[i].contains("TimesNewRoman,Italic]<12.0>")){
                    i = i + 1;
                    String first = lines[i].substring(lines[i].lastIndexOf(">") + 1);
                    sentence += first;
                    i = i + 1;
                    while(i < lines.length ){
                        sentence += lines[i];
                        if(lines[i].contains(".")){
                            break;
                        }else{
                            i++;
                        }
                    }
                    break;
                }
            }
        }
        report.setPriorRecommendation(sentence);

        //Ownership disclosure
        content = FileUtil.pdfReader(report.getFilePath(), report.getStartPage(), report.getEndPage());
        lines = content.split("\r\n");
        List<String> disclosures = new ArrayList<String>();
        for(int i = 0; i < lines.length; i++){
            String line = lines[i].trim();
            if(line.contains("long position in"))
                disclosures.add(line);
            if(line.contains("owns shares"))
                disclosures.add(line);
        }
        report.setOwnershipDisclosures(disclosures);

        //Check company
        if(report.getCompanyName().equals("")){
            content = FileUtil.pdfReaderWithFontsAndColor(report.getFilePath(), report.getStartPage(), report.getStartPage());
            lines = content.split("\r\n");
            Pattern patternCompany = Pattern.compile("TimesNewRoman,Bold\\]<30\\.0>([A-Za-z]+)");
            for (String line : lines) {
                Matcher matcherCompany = patternCompany.matcher(line);
                if (matcherCompany.find()) {
                    report.setCompanyName(matcherCompany.group(1));
                    break;
                }
            }
        }
        if(report.getAnalystNames() == null){
            content = FileUtil.pdfReader(report.getFilePath(),report.getStartPage(),report.getStartPage());
            lines = content.split("\r\n");
            Pattern patternThree = Pattern.compile("(.*)\\(([A-Z]+).*\\$[0-9]+.*\\)(.*)");
            for (int i = 0; i < lines.length; i++) {
                String line = lines[i];
                Matcher matcherThree = patternThree.matcher(line);
                if (matcherThree.find()) {
                    report.setCompanyName(matcherThree.group(1));
                    report.setTicker(matcherThree.group(2));
                    report.setRating(matcherThree.group(3));
                    report.addAnalystName(lines[i+1]);
                    break;
                }
            }
        }

        report.setInitiatingCoverage("");
        System.out.println(JSON.toJSON(report));
    }

    public static void getAnalysisReportDetailVersion2_15(AnalysisReport report) {
        String content = FileUtil.pdfReader(report.getFilePath(), report.getStartPage(), report.getStartPage());
        String[] lines = content.split("\r\n");

        //Date
        Pattern patternDate = Pattern.compile("(January|February|March|April|May|June|July|August|September|October|November|December){1}\\s*[0-9]+\\S\\s*[0-9]{4}");
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i];
            Matcher matcherDate = patternDate.matcher(line);
            if (matcherDate.find()) {
                report.setDate(matcherDate.group());
                break;
            }
        }

        //Rating
        Pattern patternRating = Pattern.compile("(Rating:){1}\\s([a-zA-Z]+)");
        Pattern patternRating2 = Pattern.compile("\\s([A-Z][a-zA-Z]+)\\s(rating){1}");
        Pattern patternRating3 = Pattern.compile("(rating|investment opinion){1}.*(to)\\s\\S([A-Z][a-zA-Z]+)");
        for (String line : lines) {
            Matcher matcherRating = patternRating.matcher(line);
            Matcher matcherRating2 = patternRating2.matcher(line);
            Matcher matcherRating3 = patternRating3.matcher(line);
            if (matcherRating.find()) {
                report.setRating(matcherRating.group(2));
                break;
            }
            if (matcherRating2.find()) {
                report.setRating(matcherRating2.group(1));
                break;
            }
            if (matcherRating3.find()) {
                report.setRating(matcherRating3.group(4));
                break;
            }
            line = line.trim();
            if(line.equals("PERFORM") || line.equals("OUTPERFORM") || line.equals("UNDERPERFORM")) {
                report.setRating(line);
                break;
            }
        }

        //Analyst name
        Pattern patternName = Pattern.compile("(.*)(\\([0-9]+\\)\\s*[0-9]+\\-[0-9]+)");
        Pattern patternName2 = Pattern.compile(".*(Analyst:)");
        for (String line : lines) {
            Matcher matcherName = patternName.matcher(line);
            Matcher matcherName2 = patternName2.matcher(line);
            if (matcherName.find()) {
                report.addAnalystName(matcherName.group(1));
            }
            if(matcherName2.find()){
                int start = matcherName2.end(1);
                report.addAnalystName(line.substring(start));
            }
        }

        if(report.getAnalystNames() == null){
            patternName = Pattern.compile("(.*)([0-9]+\\-[0-9]+\\-[0-9]+)");
            for (String line : lines) {
                Matcher matcherName = patternName.matcher(line);
                if (matcherName.find()) {
                    report.addAnalystName(matcherName.group(1));
                }
            }
        }

        //Ticker
        Pattern patternTicker = Pattern.compile("([A-Z]+)\\-([A-Za-z]+).*(\\$[0-9]+).*");
        Pattern patternTicker2 = Pattern.compile("\\((.*),\\s+(.*),\\s+(\\$[0-9]+).*\\)");
        Pattern patternTicker3 = Pattern.compile("([A-Z]+).*\\-\\s([A-Za-z]+).*(\\$[0-9]+).*");
        Pattern patternTicker4 = Pattern.compile("([A-Z]+)\\s([A-Za-z]+).*(\\$[0-9]+).*");
        for (String line : lines) {
            Matcher matcherTicker = patternTicker.matcher(line);
            Matcher matcherTicker2 = patternTicker2.matcher(line);
            Matcher matcherTicker3 = patternTicker3.matcher(line);
            Matcher matcherTicker4 = patternTicker4.matcher(line);
            if (matcherTicker.find()) {
                System.out.println("1:  " + line);
                report.setTicker(matcherTicker.group(1));
                report.setExchange(matcherTicker.group(2));
                break;
            }
            if(matcherTicker2.find()){
                System.out.println("2:  " + line);
                report.setTicker(matcherTicker2.group(1));
                report.setRating(matcherTicker2.group(2));
                break;
            }
            if (matcherTicker3.find()) {
                System.out.println("3:  " + line);
                report.setTicker(matcherTicker3.group(1));
                report.setExchange(matcherTicker3.group(2));
                break;
            }
            if (matcherTicker4.find()) {
                System.out.println("4:  " + line);
                report.setTicker(matcherTicker4.group(1));
                report.setExchange(matcherTicker4.group(2));
                break;
            }
        }

        //Prior recommendation
        String sentence = "";
        content = FileUtil.pdfReader(report.getFilePath(), report.getStartPage(), report.getStartPage(), false);
        lines = content.split("\r\n");
        for(int i = 0; i < lines.length; i++){
            String line = lines[i].trim();
            if(line.contains("Investment Opinion") || line.contains("Investment Overview")){
                sentence = line;
                i = i + 1;
                while(i < lines.length ){
                    sentence += lines[i];
                    if(lines[i].contains(".")){
                        break;
                    }else{
                        i++;
                    }
                }
                break;
            }
        }
        if(sentence.equals("")){
            content = FileUtil.pdfReaderWithFonts(report.getFilePath(), report.getStartPage(), report.getStartPage());
            lines = content.split("\r\n");
            for(int i = 0; i < lines.length; i++){
                if(lines[i].contains("TimesNewRoman,Italic]<12.0>")){
                    i = i + 1;
                    String first = lines[i].substring(lines[i].lastIndexOf(">") + 1);
                    sentence += first;
                    i = i + 1;
                    while(i < lines.length ){
                        sentence += lines[i];
                        if(lines[i].contains(".")){
                            break;
                        }else{
                            i++;
                        }
                    }
                    break;
                }
            }
        }
        if(sentence.equals("")){
            content = FileUtil.pdfReader(report.getFilePath(), report.getStartPage(), report.getStartPage(),false);
            lines = content.split("\r\n");
            for(int i = 0; i < lines.length; i++){
                if(lines[i].contains("Summary")){
                    sentence += lines[i];
                    i = i + 1;
                    while(i < lines.length ){
                        sentence += lines[i];
                        if(lines[i].contains(".")){
                            break;
                        }else{
                            i++;
                        }
                    }
                    break;
                }
            }
        }
        report.setPriorRecommendation(sentence);

        //Ownership disclosure
        content = FileUtil.pdfReader(report.getFilePath(), report.getStartPage(), report.getEndPage());
        lines = content.split("\r\n");
        List<String> disclosures = new ArrayList<String>();
        List<String> temp = new ArrayList<String>();
        for(int i = 0; i < lines.length; i++){
            String line = lines[i].trim();
            if(line.contains("long position in")) {
                int j = i + 1, k = i - 1;
                //往上和往下找整句
                while(!lines[k].contains(".") && k >= 0){
                    temp.add(lines[k]);
                    k--;
                }
                if(k >= 0)
                    temp.add(lines[k]);
                for(int m = temp.size() - 1; m > 0; m--){
                    disclosures.add(temp.get(m));
                }
                temp.clear();
                disclosures.add(line);
                while(!lines[j].contains(".") && j < lines.length){
                    disclosures.add(lines[j]);
                    j++;
                }
                if(j < lines.length)
                    disclosures.add(lines[j]);
            }
            if(line.contains("owns shares")) {
                disclosures.add(line);
                int j = i + 1, k = i - 1;
                //往上和往下找整句
                while(!lines[k].contains(".") && k >= 0){
                    temp.add(lines[k]);
                    k--;
                }
                if(k >= 0)
                    temp.add(lines[k]);
                for(int m = temp.size() - 1; m > 0; m--){
                    disclosures.add(temp.get(m));
                }
                temp.clear();
                disclosures.add(line);
                while(!lines[j].contains(".") && j < lines.length){
                    disclosures.add(lines[j]);
                    j++;
                }
                if(j < lines.length)
                    disclosures.add(lines[j]);
            }
        }
        report.setOwnershipDisclosures(disclosures);

        //CompanyName
        content = FileUtil.pdfReaderWithFontsAndColor(report.getFilePath(), report.getStartPage(), report.getStartPage());
        lines = content.split("\r\n");
        Pattern patternCompany = Pattern.compile("(TimesNewRoman\\]<30\\.0>|ArialMT\\]<24\\.0>)([A-Za-z]+)");
        for (String line : lines) {
            Matcher matcherCompany = patternCompany.matcher(line);
            if (matcherCompany.find()) {
                report.setCompanyName(matcherCompany.group(2));
                break;
            }
        }

        if(report.getCompanyName() == null){
            content = FileUtil.pdfReader(report.getFilePath(),report.getStartPage(),report.getStartPage());
            lines = content.split("\r\n");
            Pattern patternThree = Pattern.compile("(.*)\\(([A-Z]+).*\\$[0-9]+.*\\)(.*)");
            for (int i = 0; i < lines.length; i++) {
                String line = lines[i];
                Matcher matcherThree = patternThree.matcher(line);
                if (matcherThree.find()) {
                    report.setCompanyName(matcherThree.group(1));
                    report.setTicker(matcherThree.group(2));
                    report.setRating(matcherThree.group(3));
                    report.addAnalystName(lines[i+1]);
                    break;
                }
            }
        }

        if(report.getRating().equals("")){
            content = FileUtil.pdfReaderWithFontsAndColor(report.getFilePath(), report.getStartPage(), report.getStartPage());
            lines = content.split("\r\n");
            Pattern patternRatting = Pattern.compile("(ArialMT\\]<12\\.0>)([A-Za-z]+)");
            for (String line : lines) {
                Matcher matcherRating = patternRatting.matcher(line);
                if (matcherRating.find()) {
                    report.setRating(matcherRating.group(2));
                    break;
                }
            }
        }

        report.setInitiatingCoverage("");
        System.out.println(JSON.toJSON(report));
    }

    public static void getAnalysisReportDetailVersion16_35(AnalysisReport report) {
        String content = FileUtil.pdfReader(report.getFilePath(), report.getStartPage(), report.getStartPage());
        String[] lines = content.split("\r\n");

        //Date
        Pattern patternDate = Pattern.compile("(January|February|March|April|May|June|July|August|September|October|November|December){1}\\s*[0-9]+\\S\\s*[0-9]{4}");
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i];
            Matcher matcherDate = patternDate.matcher(line);
            if (matcherDate.find()) {
                report.setDate(matcherDate.group());
                break;
            }
        }

        //Ticker
        Pattern patternTicker = Pattern.compile("([A-Z]+)\\-([A-Za-z]+).*(\\$[0-9]+).*");
        Pattern patternTicker2 = Pattern.compile("\\((.*),\\s+(.*),\\s+(\\$[0-9]+).*\\)");
        Pattern patternTicker3 = Pattern.compile("([A-Z]+).*\\-\\s([A-Za-z]+).*(\\$[0-9]+).*");
        Pattern patternTicker4 = Pattern.compile("([A-Z]+)\\s([A-Za-z]+).*(\\$[0-9]+).*");
        for (String line : lines) {
            Matcher matcherTicker = patternTicker.matcher(line);
            Matcher matcherTicker2 = patternTicker2.matcher(line);
            Matcher matcherTicker3 = patternTicker3.matcher(line);
            Matcher matcherTicker4 = patternTicker4.matcher(line);
            if (matcherTicker.find()) {
                System.out.println("1:  " + line);
                report.setTicker(matcherTicker.group(1));
                report.setExchange(matcherTicker.group(2));
                break;
            }
            if(matcherTicker2.find()){
                System.out.println("2:  " + line);
                report.setTicker(matcherTicker2.group(1));
                report.setRating(matcherTicker2.group(2));
                break;
            }
            if (matcherTicker3.find()) {
                System.out.println("3:  " + line);
                report.setTicker(matcherTicker3.group(1));
                report.setExchange(matcherTicker3.group(2));
                break;
            }
            if (matcherTicker4.find()) {
                System.out.println("4:  " + line);
                report.setTicker(matcherTicker4.group(1));
                report.setExchange(matcherTicker4.group(2));
                break;
            }
        }

        //Rating
        content = FileUtil.pdfReaderWithFontsAndColor(report.getFilePath(), report.getStartPage(), report.getStartPage());
        lines = content.split("\r\n");
        Pattern patternRating = Pattern.compile("ArialNarrowMT-Bold\\]<14\\.0>([A-Za-z]+)");
        Pattern patternRating2 = Pattern.compile("ArialNarrow-Bold\\]<14\\.0>([A-Za-z]+)");
        for (String line : lines) {
            Matcher matcherRating = patternRating.matcher(line);
            Matcher matcherRating2 = patternRating2.matcher(line);
            if (matcherRating.find()) {
                report.setRating(matcherRating.group(1));
                break;
            }
            if (matcherRating2.find()) {
                report.setRating(matcherRating2.group(1));
                break;
            }
        }

        //CompanyName
        Pattern patternCompany = Pattern.compile("(GaramondLT-Book\\]<28\\.0>|GaramondLT-Book\\]<23\\.0>)(.*)");
        for (String line : lines) {
            Matcher matcherCompany = patternCompany.matcher(line);
            if (matcherCompany.find()) {
                report.setCompanyName(matcherCompany.group(2));
                break;
            }
        }

        //Analyst name
        Pattern patternTel = Pattern.compile("(.*)([0-9]+\\-[0-9]+\\-[0-9]+)");
        Pattern patternName = Pattern.compile("(ArialNarrowMT-Bold\\]<9\\.0>|ArialNarrow-Bold\\]<9\\.0>)(.*)\\[");
        Pattern patternName2 = Pattern.compile(".*(Analyst:)");
        Pattern pattern = Pattern.compile("[0-9]*");
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i];
            Matcher matcherTel = patternTel.matcher(line);
            Matcher matcherName2 = patternName2.matcher(line);
            if (matcherTel.find()) {
                for(int j = i - 1;j > 0; j--){
                    String value = lines[j];
                    Matcher matcherName = patternName.matcher(value);
                    if(matcherName.find()){
                        String originName = matcherName.group(2);
                        String[] names = originName.split("\\s");
                        for (String name : names)
                            System.out.println(name);
                        for(int k = 0; k < names.length; k++){
                            Matcher isNum = pattern.matcher(names[k]);
                            if(isNum.matches() || names[k].contains("[")) {
                                k++;
                                continue;
                            }
                            else{
                                String name = names[k];
                                if(k + 1 < names.length) {
                                    name += " " + names[k + 1];
                                }
                                if(k + 1 < names.length && names[k + 1].contains(",") && k + 2 < names.length) {
                                    name += " " + names[k + 2];
                                }
                                k += 2;
                                report.addAnalystName(name);
                            }
                        }

                        System.out.println(String.format("The analyst name line is %s", value));
                        break;
                    }
                }

            }
            if(matcherName2.find()){
                int start = matcherName2.end(1);
                report.addAnalystName(line.substring(start));
            }
        }

        if(report.getAnalystNames() == null){
            patternName = Pattern.compile("(ArialNarrowMT-Bold\\]<9\\.0>|ArialNarrow-Bold\\]<9\\.0>)(.*)");
            for (int i = 0; i < lines.length; i++) {
                String line = lines[i];
                Matcher matcherTel = patternTel.matcher(line);
                if (matcherTel.find()) {
                    for(int j = i - 1;j > 0; j--){
                        String value = lines[j];
                        Matcher matcherName = patternName.matcher(value);
                        if(matcherName.find()){
                            report.addAnalystName(matcherName.group(2));
                            System.out.println(String.format("The analyst name line is %s", value));
                            break;
                        }
                    }
                }
            }
        }

        //Prior recommendation
        String sentence = "";
        content = FileUtil.pdfReader(report.getFilePath(), report.getStartPage(), report.getStartPage(), false);
        lines = content.split("\r\n");
        for(int i = 0; i < lines.length; i++){
            String line = lines[i].trim();
            if(line.contains("Investment Opinion") || line.contains("Investment Overview")){
                sentence = line;
                i = i + 1;
                while(i < lines.length ){
                    sentence += lines[i];
                    if(lines[i].contains(".")){
                        break;
                    }else{
                        i++;
                    }
                }
                break;
            }
        }
        if(sentence.equals("")){
            content = FileUtil.pdfReader(report.getFilePath(), report.getStartPage(), report.getStartPage(),false);
            lines = content.split("\r\n");
            for(int i = 0; i < lines.length; i++){
                if(lines[i].contains("SUMMARY")){
                    i = i + 1;
                    sentence += lines[i];
                    i = i + 1;
                    while(i < lines.length ){
                        sentence += lines[i];
                        if(lines[i].contains(".")){
                            break;
                        }else{
                            i++;
                        }
                    }
                    break;
                }
            }
        }
        report.setPriorRecommendation(sentence);

        //Ownership disclosure
        content = FileUtil.pdfReader(report.getFilePath(), report.getStartPage(), report.getEndPage());
        lines = content.split("\r\n");
        List<String> disclosures = new ArrayList<String>();
        List<String> temp = new ArrayList<String>();
        for(int i = 0; i < lines.length; i++){
            String line = lines[i].trim();
            if(line.contains("long position in")) {
                int j = i + 1, k = i - 1;
                //往上和往下找整句
                while(!lines[k].contains(".") && k >= 0){
                    temp.add(lines[k]);
                    k--;
                }
                if(k >= 0)
                    temp.add(lines[k]);
                for(int m = temp.size() - 1; m > 0; m--){
                    disclosures.add(temp.get(m));
                }
                temp.clear();
                disclosures.add(line);
                while(!lines[j].contains(".") && j < lines.length){
                    disclosures.add(lines[j]);
                    j++;
                }
                if(j < lines.length)
                    disclosures.add(lines[j]);
            }
            if(line.contains("owns shares")) {
                disclosures.add(line);
                int j = i + 1, k = i - 1;
                //往上和往下找整句
                while(!lines[k].contains(".") && k >= 0){
                    temp.add(lines[k]);
                    k--;
                }
                if(k >= 0)
                    temp.add(lines[k]);
                for(int m = temp.size() - 1; m > 0; m--){
                    disclosures.add(temp.get(m));
                }
                temp.clear();
                disclosures.add(line);
                while(!lines[j].contains(".") && j < lines.length){
                    disclosures.add(lines[j]);
                    j++;
                }
                if(j < lines.length)
                    disclosures.add(lines[j]);
            }
        }
        report.setOwnershipDisclosures(disclosures);

        report.setInitiatingCoverage("");
        System.out.println(JSON.toJSON(report));
    }

    public static void process(String rootDir, String resultPath) {
        List<AnalysisReport> reports = new ArrayList<>();
        File[] Files = new File(rootDir).listFiles();
        for (File file : Files) {
            if(file.getName().endsWith(".pdf"))
                reports.addAll(getAnalysisReportPageInfo(file.getPath()));
        }

        //以下代码为写入excel
        List<String> headers = new ArrayList<>();
        headers.add("Date");
        headers.add("Broker name");
        headers.add("Analyst name");
        headers.add("Company name");
        headers.add("Ticker");
        headers.add("Exchange");
        headers.add("Rating");
        headers.add("Initiating Coverage");
        headers.add("Prior recommendation");
        headers.add("Ownership disclosure");
        headers.add("PDF file name");
        headers.add("StartPage");
        headers.add("EndPage");
        SheetModel sheetModel = FileUtil.makeSheet(resultPath, "result", headers);

        List<String[]> data = new ArrayList<>();
        for (AnalysisReport report : reports) {
            try {
                getAnalysisReportDetail(report);
            } catch (Exception e) {
                System.out.println(JSON.toJSON(report));
                e.printStackTrace();
                continue;
            }

            String[] line = new String[]{report.getDate(),
                    report.getBrokerName(),
                    JSON.toJSONString(report.getAnalystNames()),
                    report.getCompanyName(),
                    report.getTicker(),
                    report.getExchange(),
                    report.getRating(),
                    report.getInitiatingCoverage(),
                    report.getPriorRecommendation(),
                    JSON.toJSONString(report.getOwnershipDisclosures()),
                    report.getFileName(),
                    report.getStartPage() + "",
                    report.getEndPage() + ""};
            data.add(line);
        }

        FileUtil.exportSheet(sheetModel.getSheet(), data);
        FileUtil.closeSheet(sheetModel);
        System.out.println("Success!");
    }
}
